package com.gimi.cloud.bbp.manage.consumer.service.userInfo.impl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gimi.cloud.bbp.manage.consumer.feign.UserInfoFeignService;
import com.gimi.cloud.bbp.manage.consumer.service.userInfo.UserInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.EntityVerification;
import com.gimi.cloud.bbp.common.utils.JsonUtil;
import com.gimi.cloud.bbp.common.utils.StringUtil;
import com.gimi.cloud.bbp.common.utils.reqhead.Response;
import com.gimi.cloud.bbp.common.utils.reqhead.Util;
import com.gimi.cloud.bbp.common.utils.reqhead.Verification;
import com.gimi.cloud.bbp.dto.userInfo.RdUserInfoDTO;

/**
 * UserInfo的管理
 * @author chendi
 * @date 2017年12月28日
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {
	
	@Autowired
	private UserInfoFeignService userInfoFeignService;
	
    private static Logger logger = LoggerFactory.getLogger(UserInfoServiceImpl.class);

	@Override
	public Response userInfoList(String requestBody, HttpServletRequest request, HttpServletResponse response)
			throws MsgException {
		logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
		RdUserInfoDTO dto = JsonUtil.jsonToBean(requestBody,RdUserInfoDTO.class);
        Verification verification = EntityVerification.checkEntity(dto);
        if(!verification.getCheckResult()){
            Response resp = new Response();
            resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR,verification.getMsg());
            return resp;
        }else{
        	return userInfoFeignService.getUserInfoList(dto);
        }
	}

	@Override
	public Response userInfoPageList(String requestBody, HttpServletRequest request, HttpServletResponse response)
			throws MsgException {
		logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
		RdUserInfoDTO dto = JsonUtil.jsonToBean(requestBody,RdUserInfoDTO.class);
        if(StringUtil.isBlank(dto.getPageSize())||StringUtil.isBlank(dto.getCurrentPage())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        return userInfoFeignService.getUserInfoPageList(dto);
	}

	@Override
	public Response getUserInfoSingle(String requestBody, HttpServletRequest request, HttpServletResponse response)
			throws MsgException {
		logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
		RdUserInfoDTO dto = JsonUtil.jsonToBean(requestBody,RdUserInfoDTO.class);
		if (StringUtil.isBlank(dto.getUserId())) {
			throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
		}
		return userInfoFeignService.getUserInfoSingle(dto);
	}

	@Override
	public Response userInfoUpdate(String requestBody, HttpServletRequest request, HttpServletResponse response)
			throws MsgException {
		logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
		RdUserInfoDTO dto = JsonUtil.jsonToBean(requestBody,RdUserInfoDTO.class);
		if (StringUtil.isBlank(dto.getUserId())||StringUtil.isBlank(dto.getInfoId())) {
			throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
		}
		return userInfoFeignService.updateUserInfo(dto);
	}
	
}
